package helper.request.contacts;

import java.sql.*;

import server.dbconnector.DatabaseConnector;
import base.User;

public class ApproveOrRejectAddRequest implements ContactRequest {
	private static final long serialVersionUID = -4816539736494545633L;
	private int currentuserid;
	private int targetuserid;
	private boolean approve;

	public ApproveOrRejectAddRequest(int currentid,int targetid,boolean approveornot){
    	this.currentuserid=currentid;
    	this.targetuserid=targetid;
    	this.approve=approveornot;
    }
	@Override
	public User gettargetuser() {
		return null;
	}

	@Override
	public int gettargetuserid() {
		return targetuserid;
	}

	@Override
	public User getcurrentuser() {
		return null;
	}

	@Override
	public int getcurrentuserid() {
		return currentuserid;
	}
    public boolean getapprove(){
    	return this.approve;
    }
	@Override
	public boolean execute() {
		// TODO Auto-generated method stub
		return false;
	}
	public String toString(){
		if (this.approve)
			return "同意添加好友";
		else
			return "拒绝添加好友";
	}

	@Override
	public void process(DatabaseConnector dc) throws SQLException {
		int approved = 0;
		ResultSet rs=dc.query("select * from user where ID="+targetuserid);
		rs.first();
		String thisname=rs.getString(4);
		if (this.approve) {
			approved = 1;
			dc.commit("update contact set Approved=" + approved
					+ " where userIDFrom=" + currentuserid
					+ " and userIDTo=" + targetuserid);
			dc.commit("update contact set level=" + "2"
					+ " where userIDFrom=" + currentuserid
					+ " and userIDTo=" + targetuserid);
			dc.commit("insert into contact(userIDFrom,userIDTo,level,Approved)"
					+ " values(" + targetuserid + ","
					+ currentuserid + ",2," + "1)");
			dc.commit("insert into message(msgToUserID,msgFromUserID,message,ReplyNeeded) " +
					"values("+currentuserid+","+targetuserid+","+"'Contact request approved by "+thisname+"',0)");
		} else {
			dc.commit("delete from contact where userIDFrom="
					+ currentuserid + " and userIDTo="
					+ targetuserid);
			dc.commit("insert into message(msgToUserID,msgFromUserID,message,ReplyNeeded) " +
					"values("+currentuserid+","+targetuserid+","+"'Contact request declined by "+thisname+"',0)");
		}
	}
}
